package gui.menus;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import model.memento.MementoManager;

import controller.menus.AboutMenuItemController;
import controller.menus.BackLevelMenuItemController;
import controller.menus.ExitMenuItemController;
import controller.menus.HelpMenuItemController;
import controller.menus.RestartLevelMenuItemController;
import controller.menus.NextLevelMenuItemController;
import controller.menus.RedoMenuItemController;
import controller.menus.UndoMenuItemController;

import resources.GlobalSettings;

@SuppressWarnings("serial")
public class MenuBar extends JMenuBar {

	private JMenu gameMenu;
	private JMenuItem restartLevelItem;
	private JMenuItem nextLevelItem;
	private JMenuItem backLevelItem;
	private JMenuItem undoItem;
	private JMenuItem redoItem;
	private JMenuItem exitItem;
	private JMenu helpMenu;
	private JMenuItem helpItem;
	private JMenuItem aboutItem;

	public MenuBar(MenuLevelActions actionHandler, MementoManager memento) {
		super();
		this.setGameMenu(actionHandler, memento);
		this.setHelpMenu();
	}

	private void setGameMenu(MenuLevelActions actionHandler,
			MementoManager memento) {
		this.gameMenu = new JMenu(GlobalSettings.gameMenu);
		this.add(this.gameMenu);

		this.setRestartLevelItem(actionHandler);

		this.gameMenu.addSeparator();

		this.setNextLevelItem(actionHandler);
		this.setBackLevelItem(actionHandler);

		this.gameMenu.addSeparator();

		this.setUndoItem(memento);
		this.setRedoItem(memento);

		this.gameMenu.addSeparator();

		this.setExitItem();
	}

	private void setRestartLevelItem(MenuLevelActions actionHandler) {
		this.restartLevelItem = new JMenuItem(GlobalSettings.restartLevelMenuItem);
		this.restartLevelItem.addActionListener(new RestartLevelMenuItemController(
				actionHandler));
		this.gameMenu.add(this.restartLevelItem);
	}

	private void setUndoItem(MementoManager memento) {
		this.undoItem = new JMenuItem(GlobalSettings.undoMenuItem);
		this.undoItem.addActionListener(new UndoMenuItemController(
				this.undoItem, memento));
		this.gameMenu.add(this.undoItem);
	}

	private void setRedoItem(MementoManager memento) {
		this.redoItem = new JMenuItem(GlobalSettings.redoMenuItem);
		this.redoItem.addActionListener(new RedoMenuItemController(
				this.redoItem, memento));
		this.gameMenu.add(this.redoItem);
	}

	private void setNextLevelItem(MenuLevelActions actionHandler) {
		this.nextLevelItem = new JMenuItem(GlobalSettings.nextLevelMenuItem);
		this.nextLevelItem.addActionListener(new NextLevelMenuItemController(
				this.nextLevelItem, actionHandler));
		this.gameMenu.add(this.nextLevelItem);
	}

	private void setBackLevelItem(MenuLevelActions actionHandler) {
		this.backLevelItem = new JMenuItem(GlobalSettings.backLevelMenuItem);
		this.backLevelItem.addActionListener(new BackLevelMenuItemController(
				this.backLevelItem, actionHandler));
		this.gameMenu.add(this.backLevelItem);
	}

	private void setExitItem() {
		this.exitItem = new JMenuItem(GlobalSettings.exitMenuItem);
		this.exitItem.addActionListener(new ExitMenuItemController());
		this.gameMenu.add(this.exitItem);
	}

	private void setHelpMenu() {
		this.helpMenu = new JMenu(GlobalSettings.helpMenu);
		this.add(this.helpMenu);

		this.setHelpItem();
		this.setAboutItem();
	}

	private void setHelpItem() {
		this.helpItem = new JMenuItem(GlobalSettings.helpMenuItem);
		this.helpItem.addActionListener(new HelpMenuItemController());
		this.helpMenu.add(this.helpItem);
	}

	private void setAboutItem() {
		this.aboutItem = new JMenuItem(GlobalSettings.aboutMenuItem);
		this.aboutItem.addActionListener(new AboutMenuItemController());
		this.helpMenu.add(this.aboutItem);
	}
}
